﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;
using System.IO;
using InstallManager.Msi;
using System.ComponentModel;

namespace InstallerGUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        MsiInstallerSession msiInstallSession;
        string msiFile;

        public MainWindow()
        {
            InitializeComponent();
            ExtractFiles(out msiFile);
            msiInstallSession = new MsiInstallerSession(msiFile);
            if (msiInstallSession.IsInstalled)
            {
                installWizard.Visibility = Visibility.Collapsed;
                uninstallWizard.Visibility = Visibility.Visible;
            }
            else
            {
                installWizard.Visibility = Visibility.Visible;
                uninstallWizard.Visibility = Visibility.Collapsed;
            }
        }



        private void ExtractFiles(out string msiPath)
        {
            var rss = Assembly.GetExecutingAssembly().GetManifestResourceStream("InstallerGUI.ApplicationSetup.msi");
            using (var fs = File.Create(msiPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "ApplicationSetup.msi")))
            {
                rss.CopyTo(fs);
            }
        }

        private void PrepareInstall(string msiFile)
        {
            
            msiInstallSession.OnMsiMessage += new EventHandler<MsiMessageEventArgs>(msiInstallSession_OnMsiMessage);
            msiInstallSession.OnInstallCompleted += new EventHandler<InstallCompletedEventArgs>(msiInstallSession_OnInstallCompleted);
        }

        void msiInstallSession_OnInstallCompleted(object sender, InstallCompletedEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(delegate()
            {
                InstallControl.InstallFinished();
            }), null);
        }

        void msiInstallSession_OnMsiMessage(object sender, MsiMessageEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(delegate()
            {
                ProcessMsiMessage(e);
            }), null);

        }

        private void ProcessMsiMessage(MsiMessageEventArgs e)
        {
            InstallControl.OnInstallMessage(e);
        }

        private void PrepareUnInstall(string msiFile)
        {
            msiInstallSession.OnMsiMessage += new EventHandler<MsiMessageEventArgs>(msiInstallSession_OnMsiMessage2);
            msiInstallSession.OnInstallCompleted += new EventHandler<InstallCompletedEventArgs>(msiInstallSession_OnInstallCompleted2);
            
        }

        void msiInstallSession_OnInstallCompleted2(object sender, InstallCompletedEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(delegate()
            {
                UInstallControl.InstallFinished();
            }), null);
        }

        void msiInstallSession_OnMsiMessage2(object sender, MsiMessageEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(delegate()
            {
                ProcessMsiMessage2(e);
            }), null);

        }

        private void ProcessMsiMessage2(MsiMessageEventArgs e)
        {
            UInstallControl.OnInstallMessage(e);
        }
        private void AsynchInstall()
        {
            BackgroundWorker bgw = new BackgroundWorker();
            bgw.DoWork += (s, e) =>
            {
                msiInstallSession.Install();
            };
            bgw.RunWorkerAsync();
            bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
        }
        private void AsynchUninstall()
        {
            BackgroundWorker bgw = new BackgroundWorker();
            bgw.DoWork += (s, e) =>
            {
                msiInstallSession.UnInstall();

            };
            bgw.RunWorkerAsync();
            bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
        }
        void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            InstallControl.LogThis("Finished");
            InstallControl.InstallFinished();
            UInstallControl.LogThis("Finished");
            UInstallControl.InstallFinished();
        }

        private void Wizard_PageChanged(object sender, RoutedEventArgs e)
        {
            if (installWizard.CurrentPage == Page2)
            {
               
                if (!msiInstallSession.IsInstalled)
                {
                                 
                    PrepareInstall(msiFile);
                    InstallControl.InstallStarting();
                    AsynchInstall();
                }
            }
        }

        private void uninstallWizard_PageChanged(object sender, RoutedEventArgs e)
        {
            if (uninstallWizard.CurrentPage == UPage2)
            {
                if (msiInstallSession.IsInstalled)
                {
                    
                    PrepareUnInstall(msiFile);
                    UInstallControl.InstallStarting();
                    AsynchUninstall();
                }
            }
        }

       
    }
}
